﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DTO;
using DataService;

public partial class UserControls_MainContactSubmit : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            using (ServiceHelper service = new ServiceHelper())
            {
                ddlState.DataSource = service.GetStates();
                ddlCountry.DataSource = service.GetCountries();
            }

            DataBind();
        }
    }

    public string CompanyName
    {
        get { return txtCompany.Text; }
        set { txtCompany.Text = value; }
    }

    public Contact Contact
    {
        set
        {
            if (!IsPostBack)
            {
                txtFirstName.Text = value.FirstName;
                txtLastName.Text = value.LastName;
                txtEmail.Text = value.Email;
                txtPhone.Text = value.Phone;
                txtCompany.Text = value.CompanyName;
                if (value.Location != null)
                {
                    txtStreet1.Text = value.Location.Address;
                    txtStreet2.Text = value.Location.Street;
                    txtCity.Text = value.Location.City;
                    txtZipCode.Text = value.Location.ZipCode;

                    if (!string.IsNullOrEmpty(value.Location.State.Name))
                        ddlState.SelectedValue = value.Location.State.Code;

                    if (!string.IsNullOrEmpty(value.Location.Country.Name))
                        ddlCountry.SelectedValue = value.Location.Country.Code;
                }
            }
        }
        get
        {
            Contact contact = new Contact();
            contact.FirstName = txtFirstName.Text.Trim();
            contact.LastName = txtLastName.Text.Trim();
            contact.Email = txtEmail.Text.Trim();
            contact.Phone = txtPhone.Text.Trim();
            contact.CompanyName = txtCompany.Text.Trim();
            contact.Location = new LocationInfo(0)
            {
                Address = txtStreet1.Text,
                Street = txtStreet2.Text,
                City = txtCity.Text,
                ZipCode = txtZipCode.Text,
                State = new State() { Code = ddlState.SelectedValue, Name = ddlState.SelectedItem.Text },
                Country = new Country() { Code = ddlCountry.SelectedValue, Name = ddlCountry.SelectedItem.Text }
            };
            return contact;
        }
    }
}